<?php if (!defined('BASEPATH'))exit('No direct script access allowed');
/**
 * 用户个人账户控制器
 */
class Account extends MY_Controller {

    function __construct() {
        parent::__construct();
        $this->load->model('bbs_account_model');
        $this->assign("action", "account");
    }
    //用户首页
    function index() {
        if($this->user_id){
            //基本信息
            $query_params = "user_id,nickname,head_img,sex,signature";
            $user_info = $this->db->query("select $query_params from tan_users_info where user_id = ".$this->user_id." order by id desc limit 1")->row_array();
            $user_info['signature'] = $user_info['signature'] ? $user_info['signature'] : "这个人好懒，什么也没有写～";
            
            $user_info['head_img'] = strstr($user_info['head_img'],"http") ? $user_info['head_img'] : $this->img_host.$user_info['head_img'];
            //账户信息
            $user_account = $this->db->query("select a.balance finance_balance,b.balance zhu_balance from tan_account a left join tan_bbs_account b on b.user_id = a.user_id where a.user_id = ".$this->user_id." order by a.id desc limit 1")->row_array();
            $user_info['account'] = $user_account;
            //统计信息
            $user_count = $this->db->query("select * from tan_bbs_count where user_id = ".$this->user_id." order by id desc limit 1")->row_array();
            $user_count['articles_count'] = $this->db->query("select * from tan_bbs_article where authorid = ".$this->user_id." and  status = 1 order by id desc")->num_rows();
            $user_count['reply_count'] = $this->db->query("select * from tan_bbs_article_comment where uid = ".$this->user_id." and  is_show = 1 order by id desc")->num_rows();
            $user_info['bbs_count'] = $user_count;
        }else{
            $user_info = array();
        }
        $this->assign("redirectUrl", $this->get_next_request_url());
        $this->assign("user_info", $user_info);
        $this->display('account/index.html');
    }
    //溜溜珠列表
    function zhu(){
        $this->is_login();
        $bbs_account = $this->db->query("select balance from tan_bbs_account where user_id = ".$this->user_id." order by id desc limit 1")->row_array();
        $list = $this->db->query("select * from tan_bbs_zhu_detail where user_id = ".$this->user_id." order by id desc limit 10")->result_array();
        foreach ($list as $key=>$val){
            $list[$key]['create_time'] = date("Y-m-d H:s",$val['create_time']);
        }
        $this->assign("list", $list);
        $this->assign("bbs_account", $bbs_account);
        $this->display('account/zhu.html');
    }
    //我的关注
    function follows(){
        $this->is_login();
        $list = $this->db->query("select f.*,i.nickname,i.head_img,i.signature from tan_bbs_fans f left join tan_users_info i on i.user_id = f.user_id where f.followuid = ".$this->user_id." and f.is_del = 1 order by f.id desc limit 10")->result_array();
        foreach ($list as $key=>$val){
            
            $list[$key]['head_img'] = strstr($val['head_img'],"http") ? $val['head_img'] : $this->img_host.$val['head_img'];
            $list[$key]['uuid'] = encrypt_user_id($val['user_id']);
            $list[$key]['space_url'] = site_url("u/".encrypt_user_id($val['user_id']));
            $list[$key]['signature'] = $val['signature'] ? $val['signature'] : "这个人好懒，什么也没有写～";
        }
        $this->assign("list", $list);
        $this->display('account/follow.html');
    }
    //我的粉丝
    function fans(){
        $this->is_login();
        $list = $this->db->query("select f.*,i.nickname,i.head_img,i.signature from tan_bbs_fans f left join tan_users_info i on i.user_id = f.followuid where f.user_id = ".$this->user_id." and f.is_del = 1 order by f.id desc limit 10")->result_array();
        foreach ($list as $key=>$val){
            $list[$key]['uuid'] = encrypt_user_id($val['followuid']);
            //验证是否互相关注
            $is_fan = $this->bbs_user_model->get_fan_status(array("user_id"=>$val['followuid'],"followuid"=>$this->user_id,"is_del"=>1));
            $is_fan = $is_fan ? 1 : 0;
            $list[$key]['space_url'] = site_url("u/".encrypt_user_id($val['followuid']));
            $list[$key]['signature'] = $val['signature'] ? $val['signature'] : "这个人好懒，什么也没有写～";
            $list[$key]['head_img'] = strstr($val['head_img'],"http") ? $val['head_img'] : $this->img_host.$val['head_img'];
            $list[$key]['is_fan'] = $is_fan;
        }
        $this->assign("list", $list);
        $this->display('account/fan.html');
    }
    //我的收藏
    function collects(){
        $this->is_login();
        $list = $this->db->query("select f.id,f.article_id,f.is_del,a.title,a.thumb,a.readnum,a.commentnum,a.create_time from tan_bbs_favorite f left join tan_bbs_article a on a.id = f.article_id where f.user_id = ".$this->user_id." and f.is_del = 1 order by f.id desc limit 10")->result_array();
        foreach ($list as $key=>$val){
            $list[$key]['create_time'] = date("m-d H:s",$val['create_time']);
            $list[$key]['detail_url'] = site_url("p/".encrypt_article_id($val['article_id']));
        }
        $this->assign("list", $list);
        $this->display('account/collects.html');
    }
    //我的文章
    function article(){
        $this->is_login();

        $this->assign("nav", "article");
        $this->display('account/article.html');
    }
    
    //溜溜币收入
    function income(){
        $this->is_login();

        $this->assign("nav", "income");
        $this->display('account/income.html');
    }
    //溜溜币支出
    function expend(){
        $this->is_login();

        $this->assign("nav", "expend");
        $this->display('account/expend.html');
    }
    //溜溜币提现
    function withdraw(){
        $this->is_login();

        $this->assign("nav", "withdraw");
        $this->display('account/withdraw.html');
    }
    //我的问答
    function question(){
        $this->is_login();

        $this->assign("nav", "question");
        $this->display('account/question.html');
    }
    //我的回答
    function answer(){
        $this->is_login();

        $this->assign("nav", "answer");
        $this->display('account/answer.html');
    }
    //我的关注
    function follow_question(){
        $this->is_login();
        
        $this->assign("nav", "follow_question");
        $this->display('account/follow_question.html');
    }
    //我的评论
    function comment(){
        $this->is_login();
        $list = $this->db->query("select c.*,i.nickname,i.head_img,a.title,a.commentnum from tan_bbs_article_comment c left join tan_users_info i on i.user_id = c.authorid left join tan_bbs_article a on a.id = c.article_id where c.uid = ".$this->user_id." and c.is_show = 1 order by c.id desc limit 10")->result_array();
        foreach ($list as $key=>$val){
            $list[$key]["space_url"] = site_url("u/".  encrypt_user_id($val['authorid']));
            $list[$key]["detail_url"] = site_url("p/".  encrypt_article_id($val['article_id']));
            $list[$key]["content"] = mb_strlen($val['content'],"utf8") > 80 ? mb_strcut($val['content'], 0, 120, "utf8")."..." : $val['content'];
            $list[$key]['head_img'] = strstr($val['head_img'],"http") ? $val['head_img'] : $this->img_host.$val['head_img'];
//            $list[$key]["comment_url"] = date("m-d H:s",$val['create_time']);
            $list[$key]["create_time"] = date("m-d H:s",$val['create_time']);
        }
        $this->assign("list", $list);
        $this->display('account/comments.html');
    }
    //邀请好友
    function invite(){
        $this->is_login();
        $this->assign("spread_url",  site_url(encrypt_user_id($this->user_id)));
        $this->assign("body", "invite");
        $this->display('account/invite.html');
    }
    //安全中心首页
    function security(){
        $this->is_login();
        $query_params = "i.user_id,i.nickname,i.head_img,i.sex,i.signature,u.paypassword,u.mobile,u.username";
        $user_info = $this->db->query("select $query_params from tan_users_info i left join tan_users u on u.user_id = i.user_id where i.user_id = ".$this->user_id." order by i.id desc limit 1")->row_array();
        $user_info['signature'] = $user_info['signature'] ? $user_info['signature'] : "这个人好懒，什么也没有写～";
        $user_info['head_img'] = strstr($user_info['head_img'],"http") ? $user_info['head_img'] : $this->img_host.$user_info['head_img'];
        $user_info['mobile'] = $user_info['mobile'] ? mobile_formart($user_info['mobile']) : "";
        $this->assign("user_info", $user_info);
        $this->display('account/security.html');
    }
    
    //绑定手机
    function bindmobile(){
        $this->is_login();
        $this->assign("redirectUrl", isset($_REQUEST['redirectUrl']) ? $_REQUEST['redirectUrl'] : "");
        $this->display('account/bindmobile.html');
    }
    //第三方绑定
    function thirdbind(){
        $this->is_login();
        $user_info = $this->db->query("select u.user_id,qq.nickname qq_nickname,wb.nickname wb_nickname,wx.nickname wx_nickname from tan_users u left join tan_users_qq_bind qq on qq.user_id = u.user_id left join tan_users_weibo_bind wb on wb.user_id = u.user_id "
                . "left join tan_users_wx_bind wx on wx.user_id = u.user_id  where u.user_id = ".$this->user_id." order by u.user_id desc limit 1")->row_array();
        $this->assign("user_info", $user_info);
        $this->display('account/thirdbind.html');
    }
    //基本信息页面
    function baseinfo(){
        $this->is_login();
        $query_params = "user_id,nickname,head_img,sex,signature";
        $user_info = $this->db->query("select $query_params from tan_users_info where user_id = ".$this->user_id." order by id desc limit 1")->row_array();
        $user_info['signature'] = $user_info['signature'] ? $user_info['signature'] : "这个人好懒，什么也没有写～";
        $user_info['head_img'] = strstr($user_info['head_img'],"http") ? $user_info['head_img'] : $this->img_host.$user_info['head_img'];
        $this->assign("user_info", $user_info);
        $this->display('account/baseinfo.html');
    }
    //修改登录密码
    function loginpass(){
        $this->is_login();
        $this->display('account/loginpass.html');
    }
    //修改交易密码
    function paypass(){
        $this->is_login();
        $user_info = $this->db->query("select paypassword from tan_users where user_id = ".$this->user_id." order by user_id desc limit 1")->row_array();
        $this->display('account/paypass.html');
    }
    //昵称修改
    function nickname(){
        $this->is_login();
        $user_info = $this->db->query("select nickname from tan_users_info where user_id = ".$this->user_id." order by id desc limit 1")->row_array();
        $this->assign("nickname", $user_info['nickname']);
        $this->display('account/nickname.html');
    }
    //签名修改
    function signature(){
        $this->is_login();
        $user_info = $this->db->query("select signature from tan_users_info where user_id = ".$this->user_id." order by id desc limit 1")->row_array();
        $this->assign("signature", $user_info['signature'] ? $user_info['signature'] : "");
        $this->display('account/signature.html');
    }
    //是否登录
    private function is_login(){
        if(!$this->user_id){
            $url = site_url("account/login").$this->get_next_request_url();
            header("Location:".$url);
            exit;
        }
    }
}
